#!/usr/bin/env python
#
#       dnddefinitions.py
#
#       Copyright 2009 karthikeyan S. <kaarthikeyapreyan@gmail.com>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import wx

from transferdefinitions import TransferEvents

class DragandDrop:

    def enablednd(self,baseframe):

        self.Inst=baseframe
        dest = MyTextDropTarget(self.Inst.selc)
        self.Inst.selc.SetDropTarget(dest)
        wx.EVT_LIST_BEGIN_DRAG(self.Inst, self.Inst.avail.GetId(),self.dnd)

    def dnd(self,event):
        """
        Dragging  Event
        """
        trans=[]
        sels=[]
        for item in range(self.Inst.selc.GetItemCount()):
            sels.append(int(self.Inst.selc.GetItemText(item)))

        for index, entity in enumerate(map(\
            lambda id: self.Inst.avail.IsSelected(id),\
            map(lambda idx: idx, range(self.Inst.avail.GetItemCount())))):
            if entity == True and not index+1 in sels:
                trans.append(index)

        next_trans=[]
        trans.insert(0,self.Inst.selc.GetItemCount())
        for id in trans:
            if len(self.Inst.avail.GetItem(int(id),1).GetText()) >= 1:
                next_trans.append([int(id),\
                    str(self.Inst.avail.GetItem(int(id),1).GetText())])
            else:
                next_trans.append([int(id),\
                    str(self.Inst.avail.GetItem(int(id),2).GetText())])

        trans=",".join(map(lambda idx: str(idx), map(lambda id: "%s-%s"\
            %(id[0],id[1]),next_trans)))
        data = wx.PyTextDataObject(trans)
        source = wx.DropSource(self.Inst.avail)
        source.SetData(data)
        source.DoDragDrop(True)

        TransferEvents(self.Inst).selcavail()

class MyTextDropTarget(wx.TextDropTarget):

    def __init__(self,object):
        wx.TextDropTarget.__init__(self)
        self.object = object

    def OnDropText(self,x,y,data):
        """
        Override wx.TextDropTarget
        """
        data=data.split(',')
        prevcount=int(data.pop(0).split("-")[0])
        for index,item in enumerate(data):
            item=str(item).split("-")
            index=index+prevcount
            self.object.InsertStringItem(index, str(int(item[0])+1))
            self.object.SetStringItem(index,1,item[1])
